<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link href="../../component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">关键字</label>
                <div class="layui-input-inline">
                    <input type="text" name="keyWord" placeholder="商品名称" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="tradein-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="table" lay-filter="table-filter"></table>
    </div>
</div>

</body>

<!--<script type="text/html" id="table-toolbar">-->
<!--    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchReport">-->
<!--        <i class="layui-icon layui-icon-download-circle"></i>-->
<!--        导出-->
<!--    </button>-->
<!--</script>-->

<script type="text/html" id="record-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script src="../../component/layui_exts/excel.js"></script>
<script>
    layui.use(['table', 'form', 'common', 'easyAdmin'], function () {
        let easyAdmin = layui.easyAdmin;
        let table = layui.table;
        let form = layui.form;
        let MODULE_PATH = "tradein";

        let cols = [
                {
                    type: 'checkbox'
                },
                {
                    title: '入库编号',
                    field: 'invTradeinId',
                    align: 'center'
                },
                {
                    title: '商品ID',
                    field: 'invTradeinProductId',
                    align: 'center'
                },
                {
                    title: '商品名称',
                    field: 'commName',
                    align: 'center'
                },
                {
                    title: '商品规格',
                    field: 'commSpecsName',
                    align: 'center'
                },
                {
                    title: '商品类别',
                    field: 'commTypeName',
                    align: 'center'
                },
                {
                    title: '入库数量',
                    field: 'invTradeinProdSum',
                    align: 'center'
                },
                {
                    title: '入库时间',
                    field: 'invTradeinTime',
                    align: 'center'
                },
                {
                    title: '入库人员',
                    field: 'invTradeinReceive',
                    align: 'center'
                }
            ]
        ;

        easyAdmin.httpGet("/api/v1/getAdminRole",function (res) {
            if (res.data === "1") {
                let adminCols =
                    {
                        title: '操作',
                        toolbar: '#record-toolbar',
                        align: 'center'
                    };
                cols.push(adminCols);
            }

            easyAdmin.tableRender({
                elem: '#table', // 对应table的id
                url: '/inv/tradein',
                page: true, // 分页参数可以自定义
                cols: [ cols ], // 列表示
                skin: 'line', // 表格样式
                //toolbar: '#table-toolbar', // 表格头部工具栏 可定义左上角和右上角工具栏,一般用于左上角配置
                defaultToolbar: [{  // 自由配置头部工具栏右侧的图标按钮
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });
        });

        // toolbar: '#toolbarDemo'
        // toolbar 点击头部工具栏区域设定了属性为 lay-event="" 的元素时触发(包括左上角和右上角)
        table.on('toolbar(table-filter)', function (obj) {
            switch (obj.event) {
                case 'batchReport':
                    batchReportExcel(obj);
                    break;
                case 'refresh':
                    easyAdmin.tableRefresh('table');
                    break;
            }
        });

        table.on('tool(table-filter)', function (obj) {
            switch (obj.event) {
                case 'edit':
                    TradeInEdit(MODULE_PATH, obj.data.invTradeinId);
                    break;
            }
        });

        /**
         * 导出
         */
        let batchReportExcel = function (obj) {
            let ids = easyAdmin.checkField(obj, 'invTradeinId');
            let head = [];
            let headLine = [];
            headLine.push("入库编号");
            headLine.push("商品ID");
            headLine.push("商品名称");
            headLine.push("商品规格");
            headLine.push("商品类别");
            headLine.push("入库数量");
            headLine.push("入库时间");
            headLine.push("入库人员");
            head.push(headLine);
            let body = [];
            let exportData = [];
            // 获取头部
            exportData.push.apply(exportData, head)

            if (ids) {
                let idss = ids.split(',');
                let allData = table.getData(obj.config.id);
                if (allData.length !== 0) {
                    for (let i = 0; i < allData.length; i++) {
                        let line = [];
                        let invIdVal = allData[i]['invTradeinId'];
                        line.push(invIdVal);
                        line.push(allData[i]['invTradeinProductId']);
                        line.push(allData[i]['commName']);
                        line.push(allData[i]['commSpecsName']);
                        line.push(allData[i]['commTypeName']);
                        line.push(allData[i]['invTradeinProdSum']);
                        line.push(allData[i]['invTradeinTime']);
                        line.push(allData[i]['invTradeinReceive']);
                        if ($.inArray(invIdVal.toString(), idss) !== -1) {
                            body.push(line);
                        }
                    }
                }

                // 获取body
                exportData.push.apply(exportData, body)
                console.log(exportData)
                // 生成文件
                LAY_EXCEL.exportExcel(exportData, '入库-' + new Date().toLocaleString() + '.xlsx', 'xlsx')
            } else {
                let keyWordVal = $("input[name='keyWord']").val();
                console.log("====================================================" + keyWordVal)
                easyAdmin.http({
                    url: '/inv/tradein/getAll',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: "keyWord=" + keyWordVal,
                    type: 'GET',
                    success: function (result) {
                        if (result.success) {
                            let allData = result.data;
                            for (let i = 0; i < allData.length; i++) {
                                let aaa = [];
                                aaa.push(allData[i]['invTradeinId']);
                                aaa.push(allData[i]['invTradeinProductId']);
                                aaa.push(allData[i]['commName']);
                                aaa.push(allData[i]['commSpecsName']);
                                aaa.push(allData[i]['commTypeName']);
                                aaa.push(allData[i]['invTradeinProdSum']);
                                aaa.push(allData[i]['invTradeinTime']);
                                aaa.push(allData[i]['invTradeinReceive']);
                                body.push(aaa);
                            }
                        }

                        // 获取body
                        exportData.push.apply(exportData, body)
                        console.log(exportData)
                        // 生成文件
                        LAY_EXCEL.exportExcel(exportData, '入库-' + new Date().toLocaleString() + '.xlsx', 'xlsx')
                    }
                });
            }
        }

        /**
         * 跳转到edit页面
         */
        let TradeInEdit = function (MODULE_PATH, id) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['800px', '660px'],
                content: [MODULE_PATH + '/inModify.html?id=' + id, 'no']
            });
        }

        // 触发submit提交 按钮点击或者表单被执行提交时触发
        form.on('submit(tradein-query)', function (data) {
            table.reload('table', {
                // //设定异步数据接口的额外参数
                where: data.field
            })
            return false; // 阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    })
</script>
</html>
